package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.WarehouseShelfStat;
import com.ruoyi.system.service.WarehouseShelfStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 库房上架占比统计 Controller
 *
 * @author 你的名字
 */
@RestController
@RequestMapping("/system/warehouseShelfStat")
public class WarehouseShelfStatController {
    @Autowired
    private WarehouseShelfStatService warehouseShelfStatService;

    /**
     * 查询库房上架占比列表
     */
    @GetMapping("/list")
    public AjaxResult list(WarehouseShelfStat warehouseShelfStat) {
        List<WarehouseShelfStat> list = warehouseShelfStatService.selectWarehouseShelfStatList(warehouseShelfStat);
        Set<String> names = new HashSet<>();
        if(list != null && list.size() > 0)
        {
            names = list.stream().map(m -> m.getName()).collect(Collectors.toSet());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("seriesData",list);
        map.put("xData",names);
        return AjaxResult.success(map); // 封装分页结果

    }

    /**
     * 获取库房上架占比详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(warehouseShelfStatService.selectWarehouseShelfStatById(id));
    }

    /**
     * 新增库房上架占比
     */
    @PostMapping
    public AjaxResult add(@RequestBody WarehouseShelfStat warehouseShelfStat) {
        return AjaxResult.success(warehouseShelfStatService.insertWarehouseShelfStat(warehouseShelfStat));
    }

    /**
     * 修改库房上架占比
     */
    @PutMapping
    public AjaxResult edit(@RequestBody WarehouseShelfStat warehouseShelfStat) {
        return AjaxResult.success(warehouseShelfStatService.updateWarehouseShelfStat(warehouseShelfStat));
    }

    /**
     * 删除库房上架占比
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(warehouseShelfStatService.deleteWarehouseShelfStatByIds(ids));
    }

    /**
     * 批量保存库房上架占比（先删后插）
     */
    @PostMapping("/batchSave")
    public AjaxResult batchSave(@RequestBody List<WarehouseShelfStat> list) {
        return AjaxResult.success(warehouseShelfStatService.batchSaveWarehouseShelfStat(list));
    }
}